﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace WPF.MDI
{
    public class MdiManager
    {

        /// <summary>
        /// 获取指定类型的控件附加的mdi窗体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="mdiContainer"></param>
        /// <returns></returns>
        public static MdiChild GetMdiChild<T>(MdiContainer mdiContainer) where T: class
        {
            if (mdiContainer == null) return null;
            MdiChild mdiChild = mdiContainer.Children.FirstOrDefault(c => c.Content!=null&& c.Content.GetType() == typeof(T));
            return mdiChild;
        }
        /// <summary>
        /// 获取指定对象mdi窗体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="mdiContainer"></param>
        /// <param name="t"></param>
        /// <returns></returns>

        public static MdiChild GetMdiChild<T>(MdiContainer mdiContainer,T t) where T : class
        {
            if (mdiContainer == null) return null;
            MdiChild mdiChild = mdiContainer.Children.FirstOrDefault(c => c.Content!=null&& c.Content.Equals(t));
            return mdiChild;
        }

        /// <summary>
        /// 获取指定类型的控件附加的mdi窗体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="mdiContainer"></param>
        /// <returns></returns>
        public static T GetControlInMdiContainer<T>(MdiContainer mdiContainer) where T : class
        {
            if (mdiContainer == null) return null;
            MdiChild mdiChild = mdiContainer.Children.FirstOrDefault(c => c.Content!=null&& c.Content.GetType() == typeof(T));
            if (mdiChild != null) return mdiChild.Content as T;
            return null;
        }


    }
}
